/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "drawingarea.h"

static const char class_name [] = "EGUI Widget Drawingarea";

static int afc_easygui_internal_drawingarea_get_template ( DynamicClass * );
static int afc_easygui_internal_cb_drawingarea ( DynamicClass * );
static gboolean afc_easygui_internal_drawingarea_event_expose ( GtkWidget * , GdkEventExpose * , gpointer );


DynamicClass * afc_easygui_internal_drawingarea_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) {
		AFC_LOG_FAST ( AFC_ERR_NO_MEMORY );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_drawingarea_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_drawingarea );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	return ( dyn );
}

int afc_easygui_internal_drawingarea_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
	/*
	if ( dyn != NULL ) afc_dynamic_class_delete ( dyn );

	return ( AFC_ERR_NO_ERROR );
	*/
}

int afc_easygui_internal_drawingarea_get_template ( DynamicClass * dyn )
{
	dyn->result = "WIDTH HEIGHT EXPOSE";
	return ( AFC_ERR_NO_ERROR );
}

int afc_easygui_internal_cb_drawingarea ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	DynamicClass * dc = NULL;
	GtkWidget * w = NULL;
	int afc_res = AFC_ERR_NO_ERROR;
	char * width_str = NULL;
	char * height_str = NULL;
	int area_width = 100;  /* default values */
	int area_height = 100;
	char * expose_func_name = NULL;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "drawingarea" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );

	WIDGET_SET_CONTEXT ( dc, context );
	afc_easygui_widget_parse_default_params ( egui );

	width_str = afc_cmd_parser_arg_get_by_name ( context->egui->parser, "WIDTH" );
	height_str = afc_cmd_parser_arg_get_by_name ( context->egui->parser, "HEIGHT" );
	if ( width_str != NULL ) area_width = atoi ( width_str );
	if ( height_str != NULL ) area_height = atoi ( height_str );
	expose_func_name = afc_cmd_parser_arg_get_by_name ( context->egui->parser, "EXPOSE" );

	w = gtk_drawing_area_new ();

	gtk_widget_set_size_request ( w, area_width, area_height );

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) != AFC_ERR_NO_ERROR ) 
		return ( afc_res );

	if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, w, "expose_event", expose_func_name, NULL, (GtkSignalFunc) afc_easygui_internal_drawingarea_event_expose ) ) != AFC_ERR_NO_ERROR )
		return ( afc_res );


	return ( afc_res );
}


/* Event handlers */

gboolean afc_easygui_internal_drawingarea_event_expose ( GtkWidget * w, GdkEventExpose * event, gpointer data )
{
	DynamicClass * dyn = data;
	EasyGUISignalFunc sig;

	WIDGET_CALL_CALLBACK ( dyn, sig, "expose_event" );

	return ( true );
}

